package util

import (
	"gitee.com/kordar/layuiadmin/admin/enums"
	model "gitee.com/kordar/layuiadmin/admin/models"
	"github.com/flosch/pongo2/v6"
	"github.com/gin-gonic/gin"
	"github.com/kordar/gocfg"
	"net/http"
	"strings"
)

func GetPrefixCfg(section map[string]interface{}, prefix string) map[string]interface{} {
	config := map[string]interface{}{}
	for k, v := range section {
		if strings.HasPrefix(k, prefix) {
			key := strings.Replace(k, prefix, "", 1)
			config[key] = v
		}
	}
	return config
}

func Render(ctx *gin.Context, name string, data map[string]string) {
	cfg := gocfg.GetSection("goadmin.cfg")
	title := cfg["title"]
	if data["title"] != "" {
		title = data["title"]
	}
	isSuper := false
	if u, ok := ctx.Get(enums.AdminKey); ok {
		user := u.(model.SysAdmin)
		isSuper = user.Type == model.TypeSuper
	}
	ctx.HTML(http.StatusOK, name, pongo2.Context{"cfg": cfg, "title": title, "isSuper": isSuper, "name": name})
}
